﻿Imports System.Text
Imports Microsoft.VisualStudio.Utilities

Namespace VsSpeakerCompanion
	''' <summary>
	''' Provides context information to the widgets
	''' </summary>
	Public Class Context
		''' <summary>
		''' Gets the internal storage for the context properties.
		''' </summary>
        Friend Property Properties As PropertyCollection

		''' <summary>
		''' The event is raised when a VS Speaker session is starting
		''' </summary>
		Public Event SessionStarting As EventHandler

		''' <summary>
		''' The event is raised when a VS Speaker was Started
		''' </summary>
		Public Event SessionStarted As EventHandler

		''' <summary>
		''' The event is raised when a VS Speaker session is ending
		''' </summary>
		Public Event SessionEnding As EventHandler

		''' <summary>
		''' The event is raised when a VS Speaker session was ended
		''' </summary>
		Public Event SessionEnded As EventHandler

		''' <summary>
		''' Gets the VS Speaker settings. The settings are serialized between VS Speaker sessions.
		''' </summary>
        Public Property Settings As Settings

		''' <summary>
		''' Gets the VS Speaker shared settings. Every setting is loaded/saved every time so these settings are useful for consumption from different instances of VS Speaker.
		''' </summary>
        Public Property SharedSettings As Settings

		''' <summary>
		''' Returns true if the VS Speaker session is already started
		''' </summary>
        Public Property IsSessionStarted As Boolean

		Friend Sub New()
            Me.Properties = New PropertyCollection
            Me.Settings = New Settings
            Me.SharedSettings = New SharedSettings
		End Sub

		''' <summary>
		''' Returns true if the context contains a property with the specified key
		''' </summary>
		''' <param name="key"></param>
		''' <returns></returns>
		Public Function ContainsProperty(ByVal key As Object) As Boolean
			Return Me.Properties.ContainsProperty(key)
		End Function

		''' <summary>
		''' Returns the property value of type T
		''' </summary>
		''' <typeparam name="T"></typeparam>
		''' <returns></returns>
		Public Function GetProperty(Of T)() As T
			Return Me.GetProperty(Of T)(GetType(T))
		End Function

		''' <summary>
		''' Returns the property value for the specified key.
		''' </summary>
		''' <typeparam name="T"></typeparam>
		''' <param name="key"></param>
		''' <returns></returns>
		Public Function GetProperty(Of T)(ByVal key As Object) As T
			If Me.Properties.ContainsProperty(key) Then
				Return CType(Me.Properties.GetProperty(Of T)(key), T)
			End If

			Return Nothing
		End Function

		''' <summary>
		''' Adds a property value to the context.
		''' </summary>
		''' <typeparam name="T"></typeparam>
		''' <param name="value"></param>
		Public Sub AddProperty(Of T)(ByVal value As T)
			Me.AddProperty(Of T)(GetType(T), value)
		End Sub

		''' <summary>
		''' Adds a property value to the context.
		''' </summary>
		''' <typeparam name="T"></typeparam>
		''' <param name="key"></param>
		''' <param name="value"></param>
		Public Sub AddProperty(Of T)(ByVal key As Object, ByVal value As T)
			If Me.Properties.ContainsProperty(key) Then
				Me.Properties.RemoveProperty(key)
			End If

			Me.Properties.AddProperty(key, value)
		End Sub

		''' <summary>
		''' Starts a VS Speaker session and raise the Start session event.
		''' </summary>
		Friend Sub StartSession()
            If Not Me.IsSessionStarted Then
                OnSessionStarting(New EventArgs)
                Me.IsSessionStarted = True
                OnSessionStarted(New EventArgs)
            End If
		End Sub

		''' <summary>
		''' Raises the SessionStarted event
		''' </summary>
		''' <param name="e"></param>
        Protected Overridable Sub OnSessionStarted(ByVal e As EventArgs)
            RaiseEvent SessionStarted(Me, e)
        End Sub

		''' <summary>
		''' Raises the SessionStarting event
		''' </summary>
		''' <param name="e"></param>
		Protected Overridable Sub OnSessionStarting(ByVal e As EventArgs)
			RaiseEvent SessionStarting(Me, e)
		End Sub

		''' <summary>
		''' Ends a VS Speaker session and raise the End session event
		''' </summary>
		Friend Sub EndSession()
			If Me.IsSessionStarted Then
                OnSessionEnding(New EventArgs)
				Me.IsSessionStarted = False
                OnSessionEnded(New EventArgs)
			End If
		End Sub

		''' <summary>
		''' Raises the SessionEnded event
		''' </summary>
		''' <param name="e"></param>
		Protected Overridable Sub OnSessionEnded(ByVal e As EventArgs)
			RaiseEvent SessionEnded(Me, e)
		End Sub

		''' <summary>
		''' Raises the SessionEnding event
		''' </summary>
		''' <param name="e"></param>
		Protected Overridable Sub OnSessionEnding(ByVal e As EventArgs)
			RaiseEvent SessionEnding(Me, e)
		End Sub
	End Class
End Namespace